<template>
  <div class="hero-top">
    <div class="swiper">
      <div class="swiper-img">
        <img :src="item" alt="" :key="index" v-for="(item,index) of swiperImgUrl">
      </div>
      <div class="btn">
        <div class="prev">
          <i class="iconfont icon-zuo"></i>
        </div>
        <div class="next">
          <i class="iconfont icon-you"></i>
        </div>
      </div>
      <div class="dots">
        <span :key="index" v-for="(item,index) of swiperImgUrl"></span>
      </div>

    </div>
    <HomeHeroSideMenu></HomeHeroSideMenu>
  </div>
</template>

<script>
import $ from 'jquery';
import HomeHeroSideMenu from "../HomeHeroSideMenu/HomeHeroSideMenu";
export default {
  name: "HomeHeroTop",
  components:{
    HomeHeroSideMenu
  },
  data() {
    return {
      swiperImgUrl: [
        'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/5084e471aa2554867cd1c9bf333a83e4.jpg?w=2452&h=920',
        'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/34151fee7afb74b91c3a39ae8d85c8d8.jpg?thumb=1&w=1226&h=460&f=webp&q=90',
        'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/a5ebb3ecd10ba5b5f1fa25125d034492.jpg?thumb=1&w=1226&h=460&f=webp&q=90',
        'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/175b22f0032803f8bdbd94590c8c6629.jpeg?thumb=1&w=1226&h=460&f=webp&q=90',
        'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/1c3e97686468231f3f78048d7f2b7fdc.jpg?thumb=1&w=1226&h=460&f=webp&q=90'
      ]
    }
  },
  mounted() {
    const _this = this;
    //轮播
    let index = 0;
    let timer = null;
    $('.dots>span').eq(0).addClass('active');

    $('.dots>span').click(function () {
      clearInterval(timer)
      index=$(this).index()
      changeImg()
    })
    $('.next').click(function () {
      clearInterval(timer);
      console.log(index)
      index++;
      if (index === _this.swiperImgUrl.length) {
        index = 0
      }
      changeImg()
    })
    $('.prev').click(function () {
      clearInterval(timer)
      console.log(index)
      index--;
      if (index < 0) {
        index = _this.swiperImgUrl.length

      }
      changeImg()
    })

    $('.swiper').bind({
      'mouseover': function () {
        clearInterval(timer)
      },
      'mouseout': function () {
        autoPlay()
      }
    })
    function changeImg() {
      $('.swiper-img>img').eq(index).fadeIn().siblings().fadeOut()
      $('.dots>span').eq(index).addClass('active').siblings().removeClass('active')

    }
    function autoPlay() {
      timer = setInterval(() => {
        index++;
        if (index === _this.swiperImgUrl.length) {
          index = 0
        }
        changeImg()
      }, 2000)
    }

    autoPlay()


  },
  methods: {}
}
</script>

<style scoped lang="scss">
.hero-top {
  width: 100%;
  height: 460px;
  background-color: white;
  position: relative;

  .swiper {
    width: 100%;
    height: 100%;

    .swiper-img {
      width: 100%;
      height: 100%;

      > img {
        display: none;
        width: 100%;
        height: 100%;
        position: absolute;
        left: 0;
        top: 0;

        &:first-child {
          display: block;
        }
      }
    }

    .btn {
      div {
        width: 41px;
        height: 69px;
        transform: translateY(-50%);
        top: 50%;
        position: absolute;
        display: flex;
        justify-content: center;
        align-items: center;

        &:hover {
          background-color: rgba(0, 0, 0, 0.5);

          i {
            color: white;
          }
        }

        i {
          font-size: 28px;
          color: #5e5e5e;
        }
      }

      .prev {
        left: 234px;
      }

      .next {
        right: 0;

      }
    }

    .dots {
      position: absolute;
      bottom: 20px;
      height: 20px;
      width: 100%;
      display: flex;
      padding: 0 30px;
      justify-content: flex-end;
      align-items: center;

      span {
        margin-left: 10px;
        border-radius: 50%;
        width: 10px;
        height: 10px;
        background-color: rgba(0, 0, 0, .4);
        border: 2px solid hsla(0, 0%, 100%, .3);

        &.active {
          background: hsla(0,0%,100%,.4);
          border-color: rgba(0,0,0,.4);
        }
        &:hover{
          background: hsla(0,0%,100%,.4);
          border-color: rgba(0,0,0,.4);
        }
      }
    }
  }
}
</style>